# scrapy 发送post请求，模拟人人网登陆
# -*- coding: utf-8 -*-
import scrapy
import json

class RenrenspiderSpider(scrapy.Spider):
    name = 'renrenSpider'
    allowed_domains = ['http://www.renren.com']
    # start_urls = ['http://www.renren.com/']

    def start_requests(self):
        '''发送登陆请求'''
        username = input("请输入人人网账号：")
        password = input("请输入密码：")
        url="http://www.renren.com/PLogin.do"
        yield scrapy.FormRequest(url=url,formdata={"email":username,"password":password},callback=self.parse)

    def parse(self, response):
        '''解析登陆后首页内容'''
        # 我的昵称
        myusername=''
        # 连续登陆天数
        my_dayage=''
        names=response.css("dd a.hd-name::attr(title)")
        if names:
            myusername=names.extract_first()
        ages=response.css("#hdLoginDays::attr(data-tips)")
        if ages:
            temp_age=ages.extract_first()
            ageOBJ=json.loads(temp_age.replace("'","\""))# 序列化之前 先将key的引号改为双引号
            my_dayage=ageOBJ["days"]

        print("您的昵称：",myusername," 连续登陆天数：",my_dayage,"天")
        print("等级：",ageOBJ["vipLevel"],"级 ","积分：",ageOBJ["score"],"分")